<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}


// 获取广告表模型
$table_ad = C::t('#erling_gridad_image#ad_image');


if($_GET['type'] == 'delete') {
    if($_GET['formhash'] != formhash()) {
        cpmsg('提交失败', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_image&pmod=admin_ad_list','error');
        exit;
    }
    $id = intval($_GET['id']);
    if($id) {
        $table_ad->delete($id);
        cpmsg('广告删除成功', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_image&pmod=admin_ad_list','succeed'); 
    }
}elseif($_GET['type'] == 'status_pass') {
    if($_GET['formhash'] != formhash()) {
        cpmsg('提交失败', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_image&pmod=admin_ad_list','error');
        exit;
    }
    $id = intval($_GET['id']);
    if($id) {
        // 获取当前广告信息
        $current_ad = $table_ad->fetch($id);

        // 检查是否存在相同sort且未过期的广告
        $existing_ad = $table_ad->fetch_by_sort_status($current_ad['sort'], '1');

        if($existing_ad && $existing_ad['id'] != $id && $existing_ad['expire_time'] > TIMESTAMP) {
            cpmsg('审核失败：当前位置 '.$current_ad['sort'].' 还有未过期的广告《'.$existing_ad['title'].'》', '','error');
            exit;
        }

        $table_ad->audit_pass($id);
        cpmsg('广告状态更新成功', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_image&pmod=admin_ad_list','succeed');
    }
} else{

// 搜索/筛选条件
$search_uid = isset($_GET['search_uid']) ? intval($_GET['search_uid']) : null;
$search_position = isset($_GET['search_position']) ? intval($_GET['search_position']) : null;
$search_status = isset($_GET['search_status']) ? daddslashes($_GET['search_status']) : null;
$search_expire_time = isset($_GET['search_expire_time']) ? daddslashes($_GET['search_expire_time']) : null;

// 获取广告列表
$page = max(1, intval($_GET['page']));
$pagesize = 20;
$start = ($page - 1) * $pagesize;

$search_data = [
    'uid' => $search_uid,
    'status' => $search_status,
];

$result = $table_ad->fetch_all($page, $pagesize, $search_data, empty($search_expire_time)?'':'-1');
$adlist = $result['data'];
$total = $result['total'];

// 分页
$multipage = multi($total, $pagesize, $page, ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_image&pmod=admin_ad_list&search_expire_time='.$search_expire_time.'&search_uid='.$search_uid.'&search_position='.$search_position.'&search_status='.$search_status);

// 输出搜索表单
showformheader('plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_image&pmod=admin_ad_list');
echo '<div class="bm" style="margin-bottom:10px;">';
echo '<div class="bm_h cl">';
echo '</div>';
echo '<div class="bm_c">';
echo '<div class="cl">';
echo '<div style="float:left;margin-right:15px;">';
echo '<label>用户UID:</label>';
echo '<input type="text" name="search_uid" value="'.$search_uid.'" class="px" style="width:80px;">';
echo '</div>';
// echo '<div style="float:left;margin-right:15px;">';
// echo '<label>广告位置:</label>';
// echo '<select name="search_position" class="ps">';
// echo '<option value="0">全部位置</option>';
// echo '<option value="2"'.($search_position==2?' selected':'').'>首页头部</option>';
// echo '<option value="3"'.($search_position==3?' selected':'').'>首页中部</option>';
// echo '<option value="4"'.($search_position==4?' selected':'').'>首页底部</option>';
// echo '<option value="5"'.($search_position==5?' selected':'').'>帖子页右侧</option>';
// echo '</select>';
// echo '</div>';
echo '<div style="float:left;margin-right:15px;">';
echo '<label>状态:</label>';
echo '<select name="search_status" class="ps">';
echo '<option value="0">全部状态</option>';
echo '<option value="1"'.($search_status==1?' selected':'').'>正常</option>';
// echo '<option value="2"'.($search_status==2?' selected':'').'>暂停</option>';
echo '<option value="3"'.($search_status==3?' selected':'').'>未审核</option>';
echo '<option value="4"'.($search_status==4?' selected':'').'>变更后未审核</option>';
echo '</select>';
echo '</div>';
echo '<div style="float:left;margin-right:15px;">';
echo '<label>是否包含过期:</label>';
echo '<select name="search_expire_time" class="ps">';
echo '<option value="1">包含</option>';
echo '<option value="0"'.(empty($search_expire_time)?' selected':'').'>不包含</option>';
echo '</select>';
echo '</div>';
echo '<div style="float:left;">';
echo '<button type="submit" name="searchsubmit" class="pn pnc" value="true"><strong>搜索刷新</strong></button>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
showformfooter();

// 新建广告入口
echo '<div class="btnbox"><a href="'.ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_image&pmod=admin_ad_edit" class="btn">+ 添加新广告</a></div>';

// 输出广告列表
showtableheader('广告列表 <span class="em">(共 '.$total.' 条记录)</span>', 'nobottom');
echo '<tr class="header">
    <th width="50">ID</th>
    <th width="150">标题</th>
    <th width="150">图片</th>
    <th width="80">用户UID</th>
    <th width="100">位置</th>
    <th width="80">消耗积分(金币)</th>
    <th width="60">位置序号</th>
    <th width="80">状态</th>
    <th width="120">过期时间</th>
    <th width="120">操作</th>
</tr>';
foreach($adlist as $ad) {
    $position_map = array(
        2 => '首页头部',
        3 => '首页中部', 
        4 => '首页底部',
        5 => '帖子页右侧'
    );
    
    echo '<tr>';
    echo '<td>'.$ad['id'].'</td>';
    echo '<td>'.$ad['title'].'</td>';
    echo '<td><a href="'.$ad['image'].'" target="_blank" title="点击预览"><img src="'.$ad['image'].'" style="max-width:100px;max-height:60px;"></a></td>';
    echo '<td>'.$ad['uid'].'</td>';
    echo '<td>'.$position_map[$ad['position']].'</td>';
    echo '<td>'.$ad['rent_credit'].'</td>';
    echo '<td>'.$ad['sort'].'</td>';
    echo '<td>'.($ad['status']==='1'?'正常':($ad['status']==='2'?'暂停':($ad['status']==='3'?'未审核':'变更后未审核'))).'</td>';
    echo '<td>'.($ad['expire_time'] ? (($ad['expire_time'] < TIMESTAMP) ? '已过期' : dgmdate($ad['expire_time'])) : '永久').'</td>';
    echo '<td>
         '.($ad['status'] != '1' ? '<a href="'.ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_image&pmod=admin_ad_list&type=status_pass&id='.$ad['id'].'&formhash='.FORMHASH.'">审核通过</a> | ' : '').'
        <a href="'.ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_image&pmod=admin_ad_edit&id='.$ad['id'].'">编辑</a> | 
        <a href="'.ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_image&pmod=admin_ad_list&type=delete&id='.$ad['id'].'&formhash='.FORMHASH.'" onclick="return confirm(\'确认删除吗？\');">删除</a>
    </td>';
    echo '</tr>';
}
showtablefooter();
echo $multipage;
}